package com.yinquan.util;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

import org.junit.Test;

import com.alibaba.fastjson.JSONObject;
import com.yishun.springboot.pojo.YzwWorkerList;

import lombok.extern.slf4j.Slf4j;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * 保存图片工具类
 * 
 * @author xiaohuan
 * @date 2022年3月28日 下午11:03:58
 *
 */
@Slf4j
public class StoreFileUtil {

	/**
	 * 传入在线地址返回需要的照片类型本地地址-保存头像照
	 * 
	 * @param yzwProjectCode      云筑网项目号
	 * @param headInlineImagePath 头像在线地址
	 * @return
	 */
	public static String storePersonHeadImgAndChangeNeedType(Integer yzwProjectCode, String headInlineImagePath) {
		String localImg = StoreFileUtil.storePersonHeadImg(yzwProjectCode, headInlineImagePath);
		localImg = FileUtill.ChangeFileType(localImg, "jpg", "jpeg");
		return localImg;
	}

	/**
	 * 传入在线地址返回需要的照片类型本地地址-保存考勤照
	 * 
	 * @param yzwProjectCode      云筑网项目号
	 * @param headInlineImagePath 头像在线地址
	 * @return
	 */
	public static String storeAttImgAndChangeNeedType(Integer yzwProjectCode, String headInlineImagePath) {
		String localImg = StoreFileUtil.storeAttImg(yzwProjectCode, headInlineImagePath);
		localImg = FileUtill.ChangeFileType(localImg, "jpg", "jpeg");
		return localImg;
	}

	/**
	 * 传入在线地址返回本地地址-保存考勤照片
	 * 
	 * @param yzwProjectCode         云筑网项目号
	 * @param issueCardPicInlinePath 近身照在线地址
	 * @return
	 */
	public static String storeAttImg(Integer yzwProjectCode, String attImgInlinePath) {
		// 所有人员信息
		HttpGet httpGet = new HttpGet();
		if (yzwProjectCode == null)
			yzwProjectCode = 000;

		String localhostAttImgPath = "D:\\uploadGwsStoreYzwImg\\attImgInlinePath\\origin\\" + yzwProjectCode;
		String resultPath = storeImg(attImgInlinePath, localhostAttImgPath);

		return resultPath;
	}

	/**
	 * 传入在线地址返回本地地址-保存考勤照片
	 * 
	 * @param projectGuid            宇泛应用ID
	 * @param issueCardPicInlinePath 近身照在线地址
	 * @return
	 */
	public static String storeYfAttImg(String projectGuid, String attImgInlinePath) {
		// 所有人员信息
		HttpGet httpGet = new HttpGet();
		if (projectGuid == null)
			projectGuid = "000";

		String localhostAttImgPath = "E:\\YufanPeopleImg\\attendance\\" + projectGuid;
		String resultPath = storeImg(attImgInlinePath, localhostAttImgPath);

		return resultPath;
	}

	/**
	 * 传入在线地址返回本地地址-保存头像照
	 * 
	 * @param yzwProjectCode      云筑网项目号
	 * @param headInlineImagePath 头像在线地址
	 * @return
	 */
	public static String storePersonHeadImg(Integer yzwProjectCode, String headInlineImagePath) {
		// 所有人员信息
		HttpGet httpGet = new HttpGet();
		if (yzwProjectCode == null)
			yzwProjectCode = 000;

		String localhostHeadImagePath = "D:\\uploadGwsStoreYzwImg\\yzwworkerlist\\headImage\\" + yzwProjectCode;
		String resultPath = storeImg(headInlineImagePath, localhostHeadImagePath);

		return resultPath;
	}

	/**
	 * 传入在线地址返回本地地址-保存头像照
	 * 
	 * @param projectGuid         宇泛应用ID
	 * @param headInlineImagePath 头像在线地址
	 * @return
	 */
	public static String storeYfPersonHeadImg(String projectGuid, String headInlineImagePath) {
		// 所有人员信息
		HttpGet httpGet = new HttpGet();
		if (projectGuid == null)
			projectGuid = "000";

		String localhostHeadImagePath = "E:\\YufanPeopleImg\\IDCard\\" + projectGuid;
		String resultPath = storeImgAndUpdateFileSuffix(headInlineImagePath, localhostHeadImagePath);

		return resultPath;
	}

	/**
	 * 传入在线地址返回本地地址-保存近身照
	 * 
	 * @param yzwProjectCode         云筑网项目号
	 * @param issueCardPicInlinePath 近身照在线地址
	 * @return
	 */
	public static String storePersonIssueImg(Integer yzwProjectCode, String issueCardPicInlinePath) {
		// 所有人员信息
		HttpGet httpGet = new HttpGet();
		if (yzwProjectCode == null)
			yzwProjectCode = 000;

		String localhostIssueCardPicPath = "D:\\uploadGwsStoreYzwImg\\yzwworkerlist\\issueCardImage\\" + yzwProjectCode;
		String resultPath = storeImg(issueCardPicInlinePath, localhostIssueCardPicPath);

		return resultPath;
	}

	/**
	 * 传入在线地址返回本地地址-保存打卡照片
	 * 
	 * @param projectGuid            宇泛应用ID
	 * @param issueCardPicInlinePath 近身照在线地址
	 * @return
	 */
	public static String storeYfPersonIssueImg(String projectGuid, String issueCardPicInlinePath) {
		// 所有人员信息
		HttpGet httpGet = new HttpGet();
		if (projectGuid == null)
			projectGuid = "000";

		String localhostIssueCardPicPath = "E:\\YufanPeopleImg\\clockIn\\" + projectGuid;
		String resultPath = storeImgAndUpdateFileSuffix(issueCardPicInlinePath, localhostIssueCardPicPath);

		return resultPath;
	}

	// 传入一个在线和一个本地地址，返回完成或者未完成
	// --Example
	// inlineImagePath:http://img.yishun618.com/am/aq/1.jpg
	// localImagePath:D:\\uploadGwsStoreYzwImg
	/**
	 * 
	 * @param inlineImagePath 注意该字段为空会抛空
	 * @param localImagePath
	 * @return
	 */
	public static String storeImg(String inlineImagePath, String localImagePath) {

		if (inlineImagePath == null)
			return null;

		// 一、创建本地路径、确定文件名称
		File file = new File(localImagePath);
		if (!file.exists()) { // 判断文件路径是否存在
			file.mkdirs(); // 不存在创建新的文件
		}

		String imageName = null;

		try {
			// 解析成为本地路径的文件名
			imageName = inlineImagePath.substring(inlineImagePath.lastIndexOf("/") + 1);
		} catch (Exception e) {
			log.error("StoreFileUtil .error:" + e.getMessage());
			return null;
		}

		localImagePath = localImagePath + "\\" + imageName;

		// 二、下载保存到本地
		HttpGet httpGet = new HttpGet();
		// 添加至下载列表
		httpGet.addItem(inlineImagePath, localImagePath);
		// 开始下载
		httpGet.downLoadByList();
		httpGet.resetList();
		return localImagePath;
	}

	/**
	 * 将文件添加后缀并且下载到本地
	 * 
	 * @param inlineImagePath
	 * @param localImagePath
	 * @return
	 */
	public static String storeImgAndUpdateFileSuffix(String inlineImagePath, String localImagePath) {

		if (inlineImagePath == null)
			return null;

		// 一、创建本地路径、确定文件名称
		File file = new File(localImagePath);
		if (!file.exists()) { // 判断文件路径是否存在
			file.mkdirs(); // 不存在创建新的文件
		}

		String imageName = null;

		try {
			// 解析成为本地路径的文件名
			imageName = inlineImagePath.substring(inlineImagePath.lastIndexOf("/") + 1);
		} catch (Exception e) {
			log.error("StoreFileUtil .error:" + e.getMessage());
			return null;
		}

		localImagePath = localImagePath + "\\" + imageName + ".jpg";

		// 二、下载保存到本地
		HttpGet httpGet = new HttpGet();
		// 添加至下载列表
		httpGet.addItem(inlineImagePath, localImagePath);
		// 开始下载
		httpGet.downLoadByList();
		httpGet.resetList();

		File file2 = new File(localImagePath);

		if (!file2.exists()) {
			return null;
		}

		return localImagePath;
	}
}
